import gtk
import async_subprocess
from connect import *

class TCL_Wrap_Connection(Connection):
    def open(self):
        switches='-H %s' % (self.host)
        if self.keyfile:
            switches='%s -k %s' % (switches,self.keyfile)
        if self.port:
            switches='%s -P %s' % (switches,self.port)
        if self.username:
            switches='%s -U %s' % (switches,self.username)
        if self.password:
            switches='%s -p %s' % (switches,self.password)
        if self.ip_version:
            switches='%s -i %s' % (switches,self.ip_version)
        if self.compression:
            switches='%s -C' % switches
        if self.socks_port:
            switches='%s -D %s' % (switches,self.socks_port)
        if self.x11:
            switches='%s -X' % switches
        if self.agentfwd:
            switches='%s -a' % switches
        if self.agent:
            switches="%s -agent" % switches
        else:
            switches="%s -noagent" % switches
        if not self.pty:
            switches='%s -T' % switches
        if not self.shell:
            switches='%s -N' % switches
        i=0
        tunnel_param={'remote':"",'local':""}
        remote_tunnels=0
        local_tunnels=0
        for tunnel in self.tunnels:
            if tunnel.remote:
                tunneltype='remote'
                if remote_tunnels > 0:
                    tunnel_param[tunneltype]='%s:' % tunnel_param[tunneltype]
                remote_tunnels += 1
            else:
                tunneltype='local'
                if local_tunnels > 0:
                    tunnel_param[tunneltype]='%s:' % tunnel_param[tunneltype]      
                local_tunnels += 1
                
            if tunnel.listen_addr:
                listen_addr=tunnel.listen_addr
            else:
                listen_addr='127.0.0.1'
            tunnel_param[tunneltype]='%ssource_addr%s:%s' % \
                (tunnel_param[tunneltype], i, listen_addr)
            tunnel_param[tunneltype]='%s:source_port%s:%s' % \
                (tunnel_param[tunneltype], i, tunnel.listen_port)
            tunnel_param[tunneltype]='%s:dest_addr%s:%s' % \
                (tunnel_param[tunneltype], i, tunnel.host_addr)
            tunnel_param[tunneltype]='%s:dest_port%s:%s' % \
                (tunnel_param[tunneltype], i,tunnel.host_port)
            i += 1
        if tunnel_param['local'] != "":
            switches="%s -L %s" % (switches, tunnel_param['local'])
        if tunnel_param['remote'] != "":
            switches="%s -R %s" % (switches, tunnel_param['remote'])
#        if sys.platform == 'win32':
#            command='tclsh.exe'
#        else:
#            command='/usr/bin/tclsh'
        command='tclsh'
        if self.host:
            command_line='%s connect.tcl %s' % (command,switches)
        else:
            sys.quit()
        self.write_log("Launch command: %s" % command_line)
        self.child=async_subprocess.Popen(command_line, 
                               shell=True, 
                               stdin=async_subprocess.PIPE, 
                               stdout=async_subprocess.PIPE)    

        